<template>
    <div class="itemcontainer">
        <div class="home_container">
            <section v-if="fatherComponent==='home'">
                <header class="top_tips">
                    <span class="top_num">{{level}}</span>
                </header>
                <div class="home_logo item_container_style"></div>
                <router-link to='/item' class="start button_style"></router-link>
            </section>
            <section v-if="fatherComponent==='item'">
                <header class="top_tips">
                    <span class="top_num">{{itemDetail[itemNum-1].topic_name}}</span>
                </header>
                <div class="item_container_style item_back">
                    <div class="item_list_container">
                        <header class="item_title">{{itemDetail[itemNum-1].topic_name}}</header>
                        <ul>
                            <li v-for="(item,index) in itemDetail[itemNum-1].topic_answer" class="item_list" @click="choose(index,item.topic_answer_id)">
                                <span class="option_style" :class="{'on':chooseNum === index }">{{calLetters(index)}}</span>
                                <span class="option_detail">{{item.answer_name}}</span>
                            </li>
                        </ul>
                    </div>
                </div>
                <span class="next_item button_style" v-if="itemNum < itemDetail.length" @click="nextItem"></span>
                <span class="submit_item button_style" v-else @click="submitAnswer"></span>
            </section>
        </div>
    </div>
</template>

<script>
import { mapState, mapActions } from "vuex";
export default {
  data() {
    return {
      chooseNum: null, //选中答案索引
      chooseId: null //选中答案id
    };
  },
  props: {
    fatherComponent: {
      type: String,
      default: ""
    }
  },
  computed: {
    ...mapState([
      "itemNum", // 第几题
      "level", // 活动周数
      "itemDetail", // 题目信息
      "timer" // 定时器
    ])
  },
  methods: {
    ...mapActions(["addNum", "initializeData"]),
    // 计算数字返回字母
    calLetters(num) {
      let letters;
      switch (num) {
        case 0:
          return "A";
        case 1:
          return "B";
        case 2:
          return "C";
        case 3:
          return "D";
      }
    },
    choose(index, id) {
      this.chooseNum = index;
      this.choosedId = id;
    },
    nextItem() {
      if (this.chooseNum !== null) {
        this.chooseNum = null;
        //保存答案, 题目索引加一，跳到下一题
        this.addNum(this.choosedId);
      } else {
        alert("您还没有选择答案哦");
      }
    },
    submitAnswer() {
      if (this.choosedNum !== null) {
        this.addNum(this.choosedId);
        clearInterval(this.timer);
        this.$router.push("score");
      } else {
        alert("您还没有选择答案哦");
      }
    }
  },
  created() {
    // 初始化信息
    this.initializeData();
  }
};
</script>

<style>
.top_tips {
  position: absolute;
  height: 7.35rem;
  width: 3.25rem;
  top: -1.3rem;
  right: 1.6rem;
  background: url(../common/images/WechatIMG2.png) no-repeat center;
  background-size: 100% 100%;
}

.top_tips .top_num {
  position: absolute;
  left: 0.48rem;
  bottom: 1.1rem;
  height: 0.7rem;
  width: 2.5rem;
  font-size: 0.6rem;
  font-family: "\9ED1\4F53";
  font-weight: 600;
  color: #a57c50;
  text-align: center;
}

.item_container_style {
  height: 11.625rem;
  width: 13.15rem;
  background-repeat: no-repeat;
  position: absolute;
  top: 4.1rem;
  left: 1rem;
}

.home_logo {
  background-image: url(../common/images/1-2.png);
  background-size: 13.142rem 100%;
  background-position: right center;
}

.item_title {
  font-size: 0.65rem;
  color: #fff;
  line-height: 0.7rem;
}

.button_style {
  display: block;
  height: 2.1rem;
  width: 4.35rem;
  background-size: 100% 100%;
  position: absolute;
  top: 16.5rem;
  left: 50%;
  margin-left: -2.4rem;
  background-repeat: no-repeat;
}

.start {
  background-image: url(../common/images/1-4.png);
}

.next_item {
  background-image: url(../common/images/2-2.png);
}

.submit_item {
  background-image: url(../common/images/3-1.png);
}

.item_back {
  background-image: url(../common/images/2-1.png);
  background-size: 100% 100%;
}

.item_list_container {
  position: absolute;
  height: 7rem;
  width: 8rem;
  top: 2.4rem;
  left: 3rem;
  -webkit-font-smoothing: antialiased;
}

.item_title {
  font-size: 0.65rem;
  color: #fff;
  line-height: 0.7rem;
}

.item_list {
  font-size: 0;
  margin-top: 0.4rem;
  width: 10rem;
}

.item_list span {
  display: inline-block;
  font-size: 0.6rem;
  color: #fff;
  vertical-align: middle;
}

.item_list .option_style {
  height: 0.725rem;
  width: 0.725rem;
  border: 1px solid #fff;
  border-radius: 50%;
  line-height: 0.725rem;
  text-align: center;
  margin-right: 0.3rem;
  font-size: 0.5rem;
  font-family: "Arial";
}

.item_list span {
  display: inline-block;
  font-size: 0.6rem;
  color: #fff;
  vertical-align: middle;
}

.item_list .option_detail {
  width: 7.5rem;
  padding-top: 0.11rem;
}

.item_list .on {
  background-color: #ffd400;
  color: #575757;
  border-color: #ffd400;
}
</style>
